//
//  JrdSearchViewController.m
//  iMandarin
//
//  Created by longcheng on 16/7/11.
//  Copyright © 2016年 Singro. All rights reserved.
//

#import "JrdSearchViewController.h"
#import "JrdCommon.h"
#import "JRDCourseCell.h"
#import "JrdCourseDetailViewController.h"
@interface JrdSearchViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>{
    NSMutableArray *courseLastest;
    NSMutableArray *courseMostPopular;
    NSMutableArray *courseReconmmend;
    NSInteger segSelectType;

}
@property (nonatomic, strong) SCBarButtonItem *leftBarItem;
@end

@implementation JrdSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.sc_navigationItem.leftBarButtonItem = self.leftBarItem;

}

- (void)loadView{
    [super loadView];
    [self configureTableView];
    [self configureNav];
    
    [self setFakeData];
}

- (void)configureTableView {

    
    self.tableView                 = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - (NAVIGATION_BAR_HEIGHT))];
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate        = self;
    self.tableView.dataSource      = self;
    self.tableView.scrollEnabled  = NO;
    self.tableView.backgroundColor = JrdBackgroundColor;//JRD_RGB(240, 240, 240);
    [self.view addSubview:self.tableView];
    
    UISearchBar *search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
    search.delegate = self;
    [self.tableView setTableHeaderView:search];

}


- (void)configureNav{
    self.leftBarItem = [[SCBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ico_back"] highlight:[UIImage imageNamed:@"btn-back-normal"] style:SCBarButtonItemStylePlain handler:^(id sender) {
        [self.navigationController popViewControllerAnimated:YES];
        //[[NSNotificationCenter defaultCenter] postNotificationName:kShowMenuNotification object:nil];
    }];
    self.sc_navigationItem.title = NSLocalizedString(@"v2_learn_statistics",nil);//@"我们学习";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
    /*
    if (segSelectType == JRD_COURSE_LASTEST) {
        return courseLastest.count;
    }else if(segSelectType == JRD_COURSE_MOST_POPULAR){
        return courseMostPopular.count;
    }else{
        return courseReconmmend.count;
    }*/
    
}
- (void)setFakeData{
    courseLastest = [[NSMutableArray alloc] init];
    courseMostPopular = [[NSMutableArray alloc] init];
    courseReconmmend = [[NSMutableArray alloc] init];
    
    //读取plist文件获取假数据
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"classCategory" ofType:@"plist"];
    courseLastest = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    
    //课程类型
    NSString *iCategoryListPath = [[NSBundle mainBundle] pathForResource:@"courseSel" ofType:@"plist"];
    courseMostPopular = [[NSMutableArray alloc] initWithContentsOfFile:iCategoryListPath];
    
    NSString *recommendPath = [[NSBundle mainBundle] pathForResource:@"classCategory" ofType:@"plist"];
    courseReconmmend = [[NSMutableArray alloc] initWithContentsOfFile:recommendPath];
    segSelectType = 0;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return JRD_COURSE_TABLE_H;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndentifier = @"courseCell2";
    NSMutableArray* value = nil;//courseLastest[indexPath.row];
    
    JRDCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[JRDCourseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        //            NSLog(@"%f/%f",cell.frame.size.width,cell.frame.size.height);
        //下划线
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 71.5, SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = JRD_RGB(200, 199, 204);
        [cell addSubview:lineView];
    }
    if(JRD_COURSE_LASTEST == segSelectType){
        value = courseLastest[indexPath.row];
    }else if(JRD_COURSE_MOST_POPULAR == segSelectType){
        value = courseMostPopular[indexPath.row];
    }else{
        value = courseReconmmend[indexPath.row];
    }
    /*JRDCourseListModel *jzCourseM = [[JRDCourseListModel alloc] init];
    jzCourseM.CourseName = [value valueForKey:@"title"];
    jzCourseM.CourseDes =[value valueForKey:@"des"];
    jzCourseM.LocalImage = [value valueForKey:@"image"];
    [cell setJzCourseM:jzCourseM];
    */
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    NSMutableArray* value = nil;
    //Go to course detail view controller
    if(JRD_COURSE_LASTEST == segSelectType){
        value = courseLastest[indexPath.row];
    }else if(JRD_COURSE_MOST_POPULAR == segSelectType){
        value = courseMostPopular[indexPath.row];
    }else{
        value = courseReconmmend[indexPath.row];
    }
    /*JRDCourseListModel *jrdCourseM = [[JRDCourseListModel alloc] init];
    jrdCourseM.CourseName = [value valueForKey:@"title"];
    jrdCourseM.CourseDes =[value valueForKey:@"des"];
    jrdCourseM.LocalImage = [value valueForKey:@"image"];*/
    
    JrdCourseDetailViewController* vc = [[JrdCourseDetailViewController alloc] init];
    //vc.coursemodel = jrdCourseM;
    //[self.navigationController pushViewController:vc animated:YES];
    
    /*if (segSelectType == 0) {
     JZCourseListModel *jzCourseM = courseSelectArray[indexPath.row];
     JZCourseDetailViewController *jzCourseDVC = [[JZCourseDetailViewController alloc] init];
     jzCourseDVC.SID = jzCourseM.SID;
     jzCourseDVC.courseId = jzCourseM.CourseID;
     [self.navigationController pushViewController:jzCourseDVC animated:YES];
     }else{
     JZCateViewController *jzCateVC = [[JZCateViewController alloc] init];
     if (indexPath.row == 0) {
     jzCateVC.cateType = @"zhibo";
     }else{
     NSDictionary *dic = _iCategoryListArray[indexPath.row-1];
     jzCateVC.cateType = @"feizhibo";
     jzCateVC.cateNameArray = [dic objectForKey:@"categoryName"];
     jzCateVC.cateIDArray = [dic objectForKey:@"categoryID"];
     }
     
     
     [self.navigationController pushViewController:jzCateVC animated:YES];
     }*/
    
}


@end
